package com.hotchpotch.activity;

import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.hotchpotch.R;
import com.hotchpotch.adapter.RecyclerRefreshAdapter;
import com.hotchpotch.base.BaseActivity;
import com.hotchpotch.inter.OnRecyclerViewItemClickListener;

/**
 * Created by Administrator on 2017/7/18 0018.
 * RecyclerView下拉刷新/上拉加载更多
 */

public class RecyclerViewRefreshActivity extends BaseActivity {
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private RecyclerRefreshAdapter adapter;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recycler_refresh;
    }

    @Override
    protected void initView() {
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe);
        swipeRefreshLayout.setColorSchemeResources(R.color.color23c751, R.color.colordf262c, R.color.orange);
        recyclerView = (RecyclerView) findViewById(R.id.recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RecyclerRefreshAdapter(this);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initEvents() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        adapter.setItemCount();
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, 3000);
            }
        });
    }

    @Override
    protected void initData() {
        adapter.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view) {
                int pos = recyclerView.getChildAdapterPosition(view);
                showToast("点击了第" + pos + "项");
            }

            @Override
            public void onItemLongClick(View view) {

            }
        });
    }
}
